/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.aal.business;

import com.aal.bean.CategoryFacadeLocal;
import com.aal.entity.Category;
import java.util.List;
import javax.ejb.EJB;
import javax.ejb.Stateless;

/**
 *
 * @author quy
 */
@Stateless
public class CategoryManagement implements CategoryManagementLocal {
    @EJB
    private CategoryFacadeLocal categoryFacade;
    public boolean createCategory(String categoryName,String categoryDescription){
        try {
        Category category = new Category();
        category.setCategoryID(Integer.MIN_VALUE);
        category.setCategoryName(categoryName);
        category.setCategoryDescription(categoryDescription);
        categoryFacade.create(category);
        }
        catch(Exception ex){
            return false;
        }
        return true;
    }
    public boolean deleteCategory(String categoryName){
        try{
        Category category = categoryFacade.findCategoryName(categoryName);
        categoryFacade.remove(category);
        }
        catch(Exception ex){
            return false;
        }
        return true;
    }
        public boolean editCategory(String categoryName,String categoryDescription ){
        try{
            Category category = categoryFacade.findCategoryName(categoryName);
            if(categoryName != null){
                category.setCategoryName(categoryName);
            }
            if(categoryDescription != null){
                category.setCategoryDescription(categoryDescription);
            }
            categoryFacade.edit(category);
        }
        catch(Exception ex){
            return false;
        }
        return true;
    }
    public Category getCategory(int categoryId){
        return categoryFacade.find(categoryId);
    }
    public List<Category> viewAll(){
        return categoryFacade.findAll();
    }
    public List<Category> searchCategory(String categoryName){
        return categoryFacade.searchCategory(categoryName);
    }
}
